package com.example.talksdk;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import UserClientSocket.Client;
import UserGlobal.GlobalData;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    AnimatorSet mAnimatorSet = new AnimatorSet();
    ViewGroup mViewGroup;
    Boolean isclickable = true;//是否能触发点击事件
    TextView mTextView;
    EditText mEditText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView mImageView = findViewById(R.id.coffee);//咖啡图片
        mEditText = findViewById(R.id.idinput);//id输入框
        mTextView = findViewById(R.id.signbuttontext);//signfloor文本
        mTextView.setTextSize(25);
        mViewGroup = findViewById(R.id.signfloor);//登录板
        mViewGroup.setOnClickListener(this);
        mImageView.setVisibility(View.INVISIBLE);
        mEditText.setVisibility(View.INVISIBLE);
        mEditText.addTextChangedListener(mTextWatcher);
        //mTextView.setVisibility(View.INVISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mImageView.setVisibility(View.VISIBLE);
                mEditText.setVisibility(View.VISIBLE);
                float mImageViewY = mImageView.getY();
                ObjectAnimator mObjectAnimator1 = ObjectAnimator.ofFloat(mImageView, "Y", 0, mImageViewY).setDuration(800);
                ObjectAnimator mObjectAnimator2 = ObjectAnimator.ofFloat(mEditText, "scaleX", 0, 1).setDuration(800);
                mAnimatorSet.playTogether(mObjectAnimator1, mObjectAnimator2);
                mAnimatorSet.start();
            }
        }, 500);
    }

    /*TextWatcher监听输入合法性*/
    TextWatcher mTextWatcher = new TextWatcher() {
        int start, count = 0;//要修改的段落

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            this.start = start;
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String regex = "\\D";//只能输入数字id
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(s);
            if (m.find()) {
                this.count = count;
            } else
                this.count = 0;
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (count != 0) {
                mEditText.setEnabled(false);
                CharSequence mCharSequence = s.subSequence(0, start);
                s.clear();
                s.append(mCharSequence);
                setfloortext(200, 18, "id only number");
                mEditText.setEnabled(true);
            }
        }
    };
    /*setfloortext函数主要用于id合法性验证*/
    /*during 翻转动画执行时间
      size 字体大小
      tipstring 翻转时显示字体*/
    void setfloortext(final int during, final int size, final String tipstring) {
        Boolean bool = false;//动画是否执行完
        mTextView.setText("");
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mViewGroup, "rotationY", 180, 0).setDuration(during);
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.play(objectAnimator3);
                animatorSet.start();
                mTextView.setRotationY(360);
            }
        });
        /*登录成功话显示success文本不翻转*/
        if (tipstring.equals("success")) {
            mTextView.setText(tipstring);
            mTextView.setTextSize(size);
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    /*如果输入id错误floor将显示错误提示*/
                    mTextView.setText(tipstring);//错误提示
                    mTextView.setTextSize(size);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mTextView.setText("");
                            ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mViewGroup, "rotationY", 180, 0).setDuration(during);
                            AnimatorSet animatorSet = new AnimatorSet();
                            animatorSet.play(objectAnimator3);
                            animatorSet.start();
                            mTextView.setRotationY(360);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    mTextView.setText("sign in");
                                    mTextView.setTextSize(25);
                                }
                            }, during);
                        }
                    }, 1500);/*提示显示时间*/
                }
            }, during);
        }
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.signfloor) {//向服务器发送输入id
            Thread t1 = new Thread() {
                @Override
                public void run() {
                    String id = mEditText.getText().toString();
                    int status = -1;//接收返回结果
                    try {
                        Client mClient = new Client(2000,0,0);

                        mClient.sendString(id);//发送id给服务器校验
                        status = mClient.receiveInt();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (status != -1) {
                        GlobalData.mHistoryId.clear();
                        GlobalData.mChatHistory.clear();
                        GlobalData.mGravityList.clear();
                        Intent mIntent = new Intent(getApplicationContext(), PersonalHome.class);
                        startActivity(mIntent);//进入好友界面
                        GlobalData.Myid = id;//记录用户id赋值
                    }
                }
            };
            if (isclickable) {
                isclickable = false;
                t1.start();
                setfloortext(300, 25, "error");
                Toast.makeText(MainActivity.this, "network error or id not unique", Toast.LENGTH_SHORT).show();
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            sleep(600 + 1500);//提示延迟
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        isclickable = true;
                    }
                }.start();
            }
        }
    }
}